package com.stylefeng.guns.rest.modular.film.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.stylefeng.guns.api.film.FilmAsyncServiceApi;
import com.stylefeng.guns.api.film.vo.ActorVo;
import com.stylefeng.guns.api.film.vo.FilmDescVo;
import com.stylefeng.guns.api.film.vo.ImgVo;
import com.stylefeng.guns.rest.common.persistence.dao.MeetActorMapper;
import com.stylefeng.guns.rest.common.persistence.dao.MeetFilmInfoMapper;
import com.stylefeng.guns.rest.common.persistence.model.MeetActor;
import com.stylefeng.guns.rest.common.persistence.model.MeetFilmInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Service(interfaceClass = FilmAsyncServiceApi.class)
public class DefaultFilmAsyncServiceImpl implements FilmAsyncServiceApi {

    @Autowired
    private MeetFilmInfoMapper meetFilmInfoMapper;

    @Autowired
    private MeetActorMapper meetActorMapper;

    private MeetFilmInfo getFilmInfo(String filmId){

        MeetFilmInfo meetFilmInfo = new MeetFilmInfo();
        meetFilmInfo.setFilmId(filmId);

        meetFilmInfo = meetFilmInfoMapper.selectOne(meetFilmInfo);

        return meetFilmInfo;
    }

    @Override
    public FilmDescVo getFilmDesc(String filmId) {

        MeetFilmInfo meetFilmInfo = getFilmInfo(filmId);

        FilmDescVo filmDescVo = new FilmDescVo();
        filmDescVo.setBiography(meetFilmInfo.getBiography());
        filmDescVo.setFilmId(filmId);

        return filmDescVo;
    }

    @Override
    public ImgVo getImgs(String filmId) {

        MeetFilmInfo meetFilmInfo = getFilmInfo(filmId);
        // 图片地址是五个以逗号为分隔的链接URL
        String filmImgStr = meetFilmInfo.getFilmImgs();
        String[] filmImgs = filmImgStr.split(",");

        ImgVo imgVo = new ImgVo();
        imgVo.setMainImg(filmImgs[0]);
        imgVo.setImg01(filmImgs[1]);
        imgVo.setImg02(filmImgs[2]);
        imgVo.setImg03(filmImgs[3]);
        imgVo.setImg04(filmImgs[4]);

        return imgVo;
    }

    @Override
    public ActorVo getDectInfo(String filmId) {

        MeetFilmInfo meetFilmInfo = getFilmInfo(filmId);

        // 获取导演编号
        Integer directId = meetFilmInfo.getDirectorId();

        MeetActor meetActor = meetActorMapper.selectById(directId);

        ActorVo actorVo = new ActorVo();
        actorVo.setImgAddress(meetActor.getActorImg());
        actorVo.setDirectorName(meetActor.getActorName());

        return actorVo;
    }

    @Override
    public List<ActorVo> getActors(String filmId) {

        return meetActorMapper.getActors(filmId);
    }


}
